<?php
/**
 * Class UserReveiveApply
 * Desc  用户申请接单员api
 * Create on 2021/8/17 17:41
 * Create by wangyafang
 */

namespace app\api\controller\xyb;

use app\admin\model\xyb\system\Config;
use app\api\model\xyb\UserWechat;
use \app\api\model\xyb\WechatOfficialAccount;
use app\api\model\xyb\UserPlat;
use app\api\service\UsersService;
use app\common\library\Sms;
use app\common\model\xyb\AdminBelong;
use think\Db;

class UserReceiveApply extends Base
{
    protected $noNeedLogin = ['submit_receive','get_receive'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\api\model\xyb\UserReceiveApply();
    }

    /**
     * desc:用户提交申请
     */
    public function submit_receive(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        $param = $this->request->param();
       //$this->auth->id;
        $agent_info = UsersService::getCommunityWhereUsersLocated($users->id);
        if(!$agent_info){
            $this->error('所在社群未开通服务');
        }
        $param['agent_id'] = $agent_info->agent_id;
        $param['user_id'] = $users->id;
        $param['app_plat'] = 'MP-WEIXIN';
        if(!Sms::check($param['mobile'], $param['code'], 'receive')){
            $this->error('验证码错误');
        }
        $submit_info = $this->model->where('user_id',$users->id)->where('agent_id',$agent_info->agent_id)->find();
        if(!$submit_info) {
            $submit_result = $this->model->allowField(true)->save($param);
            if($submit_result) {
               // $this->zero_pay($param);//
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        } else {
            $param['status'] = 0;
            $this->model->allowField(true)->save($param,['user_id' => $users->id]);
           // $this->zero_pay($param);
            $this->success('提交成功');
        }
    }

    /**
     * desc:用户申请信息
     */
    public function get_receive(){
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $users = $this->getAuthUsers();
        //$this->auth->id;
        $agent_info = UsersService::getCommunityWhereUsersLocated($users->id);
        if(!$agent_info){
            $this->error('所在社群未开通服务');
        }
        $submit_info = $this->model->where('user_id', $users->id)->where('agent_id', $agent_info->agent_id)->find();
        if(!$submit_info){
            $this->error('没有申请信息');
        }
        $this->success('提交成功', $submit_info);
    }

    private function zero_pay($param = [])
    {
        try {
            $user_id = (new AdminBelong())->where('admin_id',$param['agent_id'])->value('bind_agent_user_id');
            $wechat_id = (new UserPlat())->where('user_id',$user_id)->value('wechat_id');
            $openid = (new UserWechat())->where('id',$wechat_id)->value('openid');
            $data = [
                'openid'=>$openid,
                'form_id'=>3676,
                'template_id' => (new Config())->where('id',1)->value('application_status_notification'),
                'page'=>'/pages/Home/index',
                'data'=>[
                    'thing1' => array(
                        'value'=> '跑腿员申请',
                    ),
                    'time6' => array(
                        'value'=>date('Y-m-d H:i:s',time()),
                    ),
                    'thing7' => array(
                        'value'=>$param['name'],
                    ),
                ],
            ];
            $jsonString = (new WechatMiniPush())->PushOneMsg($data);
            $result = json_decode($jsonString, true);
                if ($result && $result['errcode'] == 0) {
                    $subscribeObj = Db::name('xyb_agent_subscribe')->where('user_id', $user_id)->where('type', 1)->find();
                    if ($subscribeObj && $subscribeObj['count'] > 0) {
                        Db::name('xyb_agent_subscribe')->where('user_id', $user_id)->where('type', 1)->setDec('count');
                    }
                }
        } catch (\think\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * desc:驳回原因
     */
    public function reject_reason()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $reject_reason = $this->model->where('user_id',$this->auth->id)->value('reject_reason');
        $this->success('请求成功',['reject_reason'=>$reject_reason]);
    }
}